// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using ValidationAspects.Exceptions;
using ValidationAspects.Sdk;

namespace ValidationAspects.Core
{
	internal class ValidatorAdaptor<T, TValue> : IValidator<T>
	{
		private readonly IValidator<TValue> _validator;

		public ValidatorAdaptor(IValidator<TValue> validator)
		{
			Enforce.IsNotNull(validator, "validator");

			if (!typeof(TValue).IsAssignableFrom(typeof(T)))
				throw new UnsupportedValidationException(string.Format("Failed to cast [type:{0}] to [type:{1}] for validation.", typeof(T), typeof(TValue)));

			_validator = validator;
		}

		#region Implementation of IValidator
		public void Invoke(T value, IValidationContext context)
		{
			_validator.Invoke(Cast<TValue>.From(value), context);
		}

		public void Invoke(object value, IValidationContext context)
		{
			_validator.Invoke(Cast<TValue>.From(value), context);
		}
		#endregion
	}
}